package medical_application;
/** CSIS 343 Group 2 Course Project
 *  @authors Agnes Oliveira, Liane Heidemann, Daniel Schraufnagel, Matthew Lee
 */
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  Constructor for the Intake form GUI
 */
public class Intake extends JFrame{
	// variables for the Intake form
	private static final long serialVersionUID = 1L;
	JButton doneButton;
	JPanel Ipanel;
	JTextArea jTextArea1, jTextArea2, jTextArea3, jTextArea4;
	static String fName, lName;
	
	public Intake(){
		this.setTitle("I N T A K E    F O R M");
		this.setFont(Main.f);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setSize(1010,700);
		this.setResizable(false);
		JPanel intakePanel = new JPanel();
		intakePanel.setLayout(new GridBagLayout());
		
		GridBagConstraints gridBagConstraints;
		
		JLabel question1 = new JLabel();
		JLabel question2 = new JLabel();
		JLabel question3 = new JLabel();
		JLabel question4 = new JLabel();
		JScrollPane jScrollPane1 = new JScrollPane();
		jTextArea1 = new JTextArea();
		jTextArea1.setFont(Main.f);
		JScrollPane jScrollPane2 = new JScrollPane();
		jTextArea2 = new JTextArea();
		jTextArea2.setFont(Main.f);
		JScrollPane jScrollPane3 = new JScrollPane();
		jTextArea3 = new JTextArea();
		jTextArea3.setFont(Main.f);
		JScrollPane jScrollPane4 = new JScrollPane();
		jTextArea4 = new JTextArea();
		jTextArea4.setFont(Main.f);
		
		JLabel questionSmoke = new JLabel();
		JRadioButton yesSmoke = new JRadioButton();
		JRadioButton noSmoke = new JRadioButton();
		JRadioButton quitSmoke = new JRadioButton();
		
		JLabel questionDrink = new JLabel();
		JRadioButton yesDrink = new JRadioButton();
		JRadioButton noDrink = new JRadioButton();
		JRadioButton quitDrink = new JRadioButton();
		
		JLabel questionPain = new JLabel();
		JRadioButton pain1 = new JRadioButton();
		JRadioButton pain2 = new JRadioButton();
		JRadioButton pain3 = new JRadioButton();
		JRadioButton pain4 = new JRadioButton();
		JRadioButton pain5 = new JRadioButton();

		setLayout(new java.awt.GridBagLayout());

		question1.setText("Present Concerns:");
		question1.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.ipadx = 211;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 0);
		intakePanel.add(question1, gridBagConstraints);

		question2.setText("Current Medical Conditions:");
		question2.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.ipadx = 211;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
		intakePanel.add(question2, gridBagConstraints);

		question3.setText("Previous Surgeries:");
		question3.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 9;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.ipadx = 211;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
		intakePanel.add(question3, gridBagConstraints);

		question4.setText("Current Medications:");
		question4.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 14;
		gridBagConstraints.gridheight = 7;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
		intakePanel.add(question4, gridBagConstraints);

		jTextArea1.setColumns(20);
		jTextArea1.setRows(5);
		jScrollPane1.setViewportView(jTextArea1);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridheight = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.ipadx = 222;
		gridBagConstraints.ipady = 75;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
		intakePanel.add(jScrollPane1, gridBagConstraints);

		jTextArea2.setColumns(20);
		jTextArea2.setRows(5);
		jScrollPane2.setViewportView(jTextArea2);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 6;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridheight = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.ipadx = 222;
		gridBagConstraints.ipady = 79;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
		intakePanel.add(jScrollPane2, gridBagConstraints);

		jTextArea3.setColumns(20);
		jTextArea3.setRows(5);
		jScrollPane3.setViewportView(jTextArea3);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 11;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridheight = 3;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.ipadx = 222;
		gridBagConstraints.ipady = 86;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(5, 10, 0, 0);
		intakePanel.add(jScrollPane3, gridBagConstraints);

		jTextArea4.setColumns(20);
		jTextArea4.setRows(5);
		jScrollPane4.setViewportView(jTextArea4);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 22;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.gridheight = 8;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.ipadx = 222;
		gridBagConstraints.ipady = 73;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		gridBagConstraints.insets = new java.awt.Insets(6, 10, 11, 0);
		intakePanel.add(jScrollPane4, gridBagConstraints);
		
		/** CHECKBOX **/
		questionSmoke.setText("Tobacco?");
		questionSmoke.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.ipadx = 93;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(25, 102, 0, 0);
		intakePanel.add(questionSmoke, gridBagConstraints);

		yesSmoke.setText("Yes");
		yesSmoke.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(2, 136, 0, 0);
		intakePanel.add(yesSmoke, gridBagConstraints);

		noSmoke.setText("No");
		noSmoke.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 3;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 136, 0, 0);
		intakePanel.add(noSmoke, gridBagConstraints);

		quitSmoke.setText("Quit");
		quitSmoke.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 4;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 136, 0, 0);
		intakePanel.add(quitSmoke, gridBagConstraints);
		
		questionDrink.setText("Alcohol?");
		questionDrink.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 6;
		gridBagConstraints.ipadx = 93;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(21, 102, 0, 0);
		intakePanel.add(questionDrink, gridBagConstraints);

		yesDrink.setText("Yes");
		yesDrink.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 7;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(4, 136, 0, 0);
		intakePanel.add(yesDrink, gridBagConstraints);

		noDrink.setText("No");
		noDrink.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 8;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 136, 0, 0);
		intakePanel.add(noDrink, gridBagConstraints);

		quitDrink.setText("Quit");
		quitDrink.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 9;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 136, 0, 0);
		intakePanel.add(quitDrink, gridBagConstraints);

		questionPain.setText("Pain Rating Scale:");
		questionPain.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 11;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.ipadx = 90;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(5, 111, 0, 0);
		intakePanel.add(questionPain, gridBagConstraints);

		pain1.setText("No Pain");
		pain1.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 12;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(10, 142, 0, 0);
		intakePanel.add(pain1, gridBagConstraints);

		pain2.setText("Mild Pain");
		pain2.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 13;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 142, 0, 0);
		intakePanel.add(pain2, gridBagConstraints);

		pain3.setText("Moderate Pain");
		pain3.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 14;
		gridBagConstraints.gridwidth = 2;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 142, 0, 0);
		intakePanel.add(pain3, gridBagConstraints);

		pain4.setText("Severe Pain");
		pain4.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 15;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.gridheight = 6;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 142, 0, 113);
		intakePanel.add(pain4, gridBagConstraints);

		pain5.setText("Worst Pain Possible");
		pain5.setFont(Main.f);
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 2;
		gridBagConstraints.gridy = 26;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.gridheight = 8;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
		gridBagConstraints.insets = new java.awt.Insets(0, 142, 0, 113);
		intakePanel.add(pain5, gridBagConstraints);
		
		doneButton = new JButton("Done");
		doneButton.setFont(Main.f);
		gridBagConstraints.gridx = 3;
		gridBagConstraints.gridy = 25;
		gridBagConstraints.gridwidth = 3;
		gridBagConstraints.gridheight = 8;
		gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
		gridBagConstraints.insets = new java.awt.Insets(0, 142, 0, 113);
		intakePanel.add(doneButton, gridBagConstraints);
		 
		actionDone();
		this.add(intakePanel);
		this.setVisible(true);
		this.setLocationRelativeTo(null);
	}//end of default constructor
	
	/**
	 * This method calls the writeFile function and does a password check before 
	 * the window can be closed.
	 */
	public void actionDone(){
		doneButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				JPasswordField strPassword = new JPasswordField(10);
				boolean flag = false;
				do {
					JOptionPane.showConfirmDialog(null, strPassword,
							"Enter Password:", JOptionPane.OK_CANCEL_OPTION);
					String pass = new String(strPassword.getPassword());
					if (pass.equals(LoginGUI.PASSWORD)) {
						writeFile();
						dispose();
						flag = true;
					} else {
						JOptionPane.showMessageDialog(null,
								"Wrong login and/or password", "W R O N G",
								JOptionPane.ERROR_MESSAGE);
					}
				} while (!flag);
			}
		});
	}//end of actionDone
	
	/**
	 * This method saves the patient's intake information to a .txt file
	 */
	public void writeFile(){
		try{
			SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
			String date = sdf.format(new Date());
			String fileName = fName + ", " + lName + " " + date + ".txt";
			File file = new File(fileName);
			BufferedWriter out = new BufferedWriter(new FileWriter("src/medical_application/intakes/" + file));
			out.write("Date: " + date + "\n");
			out.newLine(); out.write("Patient First: " + fName);
			out.newLine(); out.write("Patient Last:  " + lName);
			out.newLine(); out.write("----------------------------------------------");
			out.newLine(); out.write("Present Concerns:");
			out.newLine(); out.write( "\t" + jTextArea1.getText());
			out.newLine(); out.write("Current Medical Conditions:");
			out.newLine(); out.write("\t" + jTextArea2.getText());
			out.newLine(); out.write("Previous Surguries:");
			out.newLine(); out.write("\t" +  jTextArea3.getText());
			out.newLine(); out.write("\nCurrent Medications: ");
			out.newLine(); out.write("\t" + jTextArea4.getText());
			out.close();
		}catch(IOException e) {
			JOptionPane.showMessageDialog(null, "I/O Error");
		}catch(ArrayIndexOutOfBoundsException e){
			JOptionPane.showMessageDialog(this, "Array Index Error");
		}
	}//end of writeFile
	
}//end of intake class
